package com.android.enjoymax;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ListView;

import com.google.android.maps.GeoPoint;
import com.android.enjoymax.Map.MyItemizedOverlay;

import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.yelp.v2.Business;
import com.yelp.v2.Categories;
import com.yelp.v2.Center;

public class GetEvents {
	public static final String URL = "http://api.yelp.com/business_review_search";
	public static final String KEY = "OtMtfEOAmZq_Bk3GJqz37Q";
	public String RADIUS = "10";
	public String LIMIT = "5";
	public String KEYWORDS = ""; 
	
	double latitude = 0.0;
	double longitude = 0.0;
	private MyItemizedOverlay overlay;
	private List<Overlay> mapOverlay;
	
	ArrayList<Business> foodPlaces = new ArrayList<Business>(); 
	String[] values;
	
	private ListView listView;
	private Context cont;
	
    public synchronized void Events(Context c, String keywords, MyItemizedOverlay overlay, List<Overlay> mapOverlays, Center location, double radius, int limit) {
    	this.latitude = location.getLatitude(); 
    	this.longitude = location.getLongitude();
    	this.KEYWORDS = keywords;
    	this.RADIUS = String.valueOf(radius); 
    	this.LIMIT = String.valueOf(limit); 
    	this.overlay = overlay;
    	this.mapOverlay = mapOverlays;
    	cont = c;
        new RequestTask().execute(new String[] {URL, "FOOD_ONLY"});
    } 
    
    class RequestTask extends AsyncTask<String, Void, String>{
    	
        private static final String TAG = "GETEVENTS";

		@Override
        protected String doInBackground(String... uri) {
			String response = "";
			
        	if (uri[1].equals("FOOD_ONLY")) {
        		RestClient client = new RestClient(uri[0]);
        		client.addParam("term", KEYWORDS);
        		client.addParam("lat", String.valueOf(latitude));
        		client.addParam("long", String.valueOf(longitude));
        		client.addParam("radius", RADIUS);
        		client.addParam("limit", LIMIT);
        		client.addParam("ywsid", KEY);
        		try {
        		    client.Execute();
        		} 
        		catch (Exception e) {
        		    e.printStackTrace();
        		    return "";
        		}
        		response = client.getResponse();
        		onProcessString(response);
        	}
        	return response;
        }

        private void onProcessString(String result) {
            super.onPostExecute(result);
            try {
            	if (result == "" || result.length() == 0) return;

            	JSONObject jsonObject = new JSONObject(result);
				JSONArray array = jsonObject.getJSONArray("businesses");
				ArrayList<NameValuePair> latLonList = new ArrayList<NameValuePair>();
				
				for(int i = 0 ; i < array.length() ; i++){
					Business tempVar = new Business(); 
					tempVar.setId(array.getJSONObject(i).getString("id")); 
					tempVar.setName(array.getJSONObject(i).getString("name"));
					tempVar.setMobileUrl(array.getJSONObject(i).getString("mobile_url")); 
					tempVar.setImageUrl(array.getJSONObject(i).getString("photo_url")); 
					tempVar.setImageUrlSmall(array.getJSONObject(i).getString("photo_url_small")); 
					tempVar.setReviewCount(Integer.valueOf(array.getJSONObject(i).getString("review_count"))); 
					tempVar.setPhone(array.getJSONObject(i).getString("phone"));
					Center location = new Center(); 
					GeoPoint p = Map.coordinatesToGeoPoint(location.getLatitude(), location.getLongitude());
					
					OverlayItem overlayItem = new OverlayItem(p, "", "");
					
					overlay.addOverlay(overlayItem);
			        mapOverlay.add(overlay);
					
					location.setLatitude(Double.valueOf(array.getJSONObject(i).getString("latitude"))); 
					location.setLongitude(Double.valueOf(array.getJSONObject(i).getString("longitude"))); 
					tempVar.setLocation(location); 
 /**
					ArrayList<Categories> categories = new ArrayList<Categories>();
					JSONArray categoryArray = new JSONArray(array.getJSONObject(i).getString("categories"));
					
					for (int j = 0; j < categoryArray.length(); j++) {
						Categories tempCategory = new Categories(); 
						tempCategory.setCategory_filter(categoryArray.getJSONObject(i).getString("category_filter"));
						tempCategory.setName(categoryArray.getJSONObject(i).getString("name")); 
						tempCategory.setSearch_url(categoryArray.getJSONObject(i).getString("search_url")); 
						categories.add(tempCategory); 
					}
					
					tempVar.setCategories(categories);
					**/
					tempVar.setUrl(array.getJSONObject(i).getString("url"));
					Log.d(TAG,"TempVar is " + values[i]);
					foodPlaces.add(tempVar); 
				}
				
				values = new String[foodPlaces.size()];
				
				for (int i = 0; i < values.length - 1 ; i++) {
					values[i] = foodPlaces.get(i).getName();
					Log.d(TAG,"Name is " + values[i]);
				}
				
				//Map.setListView(cont,values);
				
			} 
            catch (JSONException e) {
				e.printStackTrace();
			}
        }
    }
}
